unit UntSelSalesmen;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, DBGridEhGrouping, GridsEh, DBGridEh, RzButton, DB, MemDS, DBAccess,
  MyAccess,UntDMDB, BaseFrm, DBClient, Provider;

type
  TFrmSelSalesmen = class(TBaseForm)
    dbgdsalesmen: TDBGridEh;
    btnOK: TRzButton;
    btnCancel: TRzButton;
    myqrySalesmen: TMyQuery;
    dsSalesmen: TDataSource;
    dsPrdSelSalesment: TDataSetProvider;
    cdsSelSalesmen: TClientDataSet;
    cdsSelSalesmenuserName: TWideStringField;
    cdsSelSalesmenstaffid: TWideStringField;
    cdsSelSalesmensel: TBooleanField;
    procedure btnCancelClick(Sender: TObject);
    procedure btnOKClick(Sender: TObject);
    procedure dbgdsalesmenDblClick(Sender: TObject);
    procedure dbgdsalesmenKeyPress(Sender: TObject; var Key: Char);
    procedure FormShow(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure cdsSelSalesmenAfterOpen(DataSet: TDataSet);
  private
    FuserNameList: TStringList;
    { Private declarations }
  public
    class function Select: TStringList;
    function selectUser: Boolean;
    { Public declarations }
  end;



implementation
 var
  FrmSelSalesmen: TFrmSelSalesmen;
{$R *.dfm}

procedure TFrmSelSalesmen.btnCancelClick(Sender: TObject);
begin
  ModalResult := mrCancel;
end;

procedure TFrmSelSalesmen.btnOKClick(Sender: TObject);
begin
  if(selectUser()) then
  begin
  ModalResult := mrOk;
  end;
end;

procedure TFrmSelSalesmen.cdsSelSalesmenAfterOpen(DataSet: TDataSet);
begin
  inherited;
  with cdsSelSalesmen do
  begin
    DisableControls;
    try
      First;
      while not Eof do
      begin
        edit;
        FieldByName('sel').AsBoolean := false;
        post;
        Next;
      end;
    finally
      EnableControls;
    end;
  end;
end;

procedure TFrmSelSalesmen.dbgdsalesmenDblClick(Sender: TObject);
begin
  //if(selectUser()) then
  //begin
  //  ModalResult := mrOk;
  //end;
end;

procedure TFrmSelSalesmen.dbgdsalesmenKeyPress(Sender: TObject; var Key: Char);
begin
  if key=#13 then
  begin
    btnOK.Click;
  end;
end;

procedure TFrmSelSalesmen.FormDestroy(Sender: TObject);
begin
  inherited;
  if Assigned(FuserNameList) then
  begin
    FreeAndNil(FuserNameList);
  end;
end;

procedure TFrmSelSalesmen.FormShow(Sender: TObject);
begin
  FrmSelSalesmen.dbgdsalesmen.SetFocus;
end;

class function TFrmSelSalesmen.Select: TStringList;
begin
  Result := nil;
  if not Assigned(FrmSelSalesmen) then
  begin
    FrmSelSalesmen := TFrmSelSalesmen.Create(nil);
    FrmSelSalesmen.FuserNameList := TStringList.Create;
  end;
  FrmSelSalesmen.FuserNameList.Clear;
  FrmSelSalesmen.cdsSelSalesmen.Open;
  if FrmSelSalesmen.ShowModal= mrOk then
  begin
    Result := FrmSelSalesmen.FuserNameList;
  end;
  FrmSelSalesmen.cdsSelSalesmen.Close;
end;

function TFrmSelSalesmen.selectUser: Boolean;
begin
  Result := False;
  FuserNameList.Clear;
  with cdsSelSalesmen do
  begin
    DisableControls;
    try
      First;
      while not Eof do
      begin
        if (FieldByName('sel').AsBoolean) then
        begin
          FuserNameList.Add(FieldByName('userName').AsString) ;
        end;
        Next;
      end;
    finally
      EnableControls;
    end;
  end;
  if (FuserNameList.Count>0)   then
  begin
    Result := True ;
  end;
end;

initialization

finalization
if(Assigned(FrmSelSalesmen))then
begin
  FreeAndNil(FrmSelSalesmen);
end;

end.

